package com.maotai.app.servlet;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.maotai.app.service.MaotaiAppService;
import com.maotai.llzhou.util.GetUUID;


@Controller 
public class MaotaiAppController {
	private final Log log = LogFactory.getLog(MaotaiAppController.class);
    
    @Resource
	private MaotaiAppService maotaiAppService;
    
    @ResponseBody
	@RequestMapping(value="/insertAliHuaKuaiSession",produces = {"application/text;charset=UTF-8"}) 
	public String insertAliHuaKuaiSession(@RequestBody String param)
	{
    	System.out.println(param);
    	Map<String,String> map = new HashMap<String,String>();
		map.put("alisessionid", GetUUID.getId());
		map.put("alisessionitem", param);
		int count = 0;
    	try {
    		count = maotaiAppService.insertAliHuaKuaiSession(map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count+"";
	}
    @ResponseBody
	@RequestMapping(value="/getAliHuaKuaiSessionOne",produces = {"application/text;charset=UTF-8"}) 
	public String getAliHuaKuaiSessionOne(@RequestBody String param)
	{
    	Map<String, Object> map = new HashMap<String,Object>();
		try {
			map = maotaiAppService.getAliHuaKuaiSessionOne();
			maotaiAppService.deleteAliHuaKuaiSessionOne(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(map!=null)
			return map.get("alisessionitem")+"";
		else return  "";
	}
    @ResponseBody
	@RequestMapping(value="/getMaotaiAppOrderItem",produces = {"application/text;charset=UTF-8"}) 
	public String getMaotaiAppOrderItem(@RequestBody String param)
	{
    	String returtnstr = "";
    	try {
			List<Map<String, Object>> list = maotaiAppService.getMaotaiAppOrderItem();
			if(list!=null && list.size()>0){
				JSONArray jArr = JSONArray.fromObject(list);
				returtnstr = jArr.toString();
			}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return returtnstr;
	}
    @ResponseBody
	@RequestMapping(value="/queryMaotaiAppOrderItem",produces = {"application/json;charset=UTF-8"}) 
	public String queryMaotaiAppOrderItem(@RequestBody String param)
	{
    	String returtnstr = "{}";
    	String[] dates = param.split("&");
//    	JSONObject jj = JSONObject.fromObject(param);
//    	String startDate = jj.getString("startDate");
//    	String endDate = jj.getString("endDate");
    	String startDate = dates[0].split("=")[1];
    	String endDate = dates[1].split("=")[1];
    	String area = dates[2].split("=")[1];
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("startDate", startDate);
    	map.put("endDate", endDate);
    	if("bj".equals(area)){
    		map.put("area", "北京");
    	}else if("sh".equals(area)){
    		map.put("area", "上海");
    	}else if("ah".equals(area)){
    		map.put("area", "安徽");
    	}
    	try {
			List<Map<String, Object>> list = maotaiAppService.queryMaotaiAppOrderItem(map);
			if(list!=null && list.size()>0){
				JSONArray jArr = JSONArray.fromObject(list);
				returtnstr = jArr.toString();
			}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return returtnstr;
	}
   
}
